package com.atguigu.cf.test;

import java.util.concurrent.*;

/**
 * author: 封捷
 * date: 2023/1/29 16:33
 * version: 1.0
 */
public class ThreadPoolProvider {

    public static ExecutorService getThreadPool() {

        // 核心线程数
        int corePoolSize = 3;

        // 最大线程数
        int maximumPoolSize = 5;

        // 非核心线程最大空闲时间：数量
        long keepAliveTime = 10;

        // 非核心线程最大空闲时间：单位
        TimeUnit unit = TimeUnit.SECONDS;

        // 存放等待中任务的阻塞队列
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);

        // 创建线程对象的工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();

        // 拒绝策略
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

        // 创建线程池
        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler);
    }

}
